package test.Sorting;

//二分法查找
class BinarySearch {

    public static int[] nums = {1,4,5,6,8,15,23,31,56,94,106};

    public static int search(int[] nums, int target) {
        int i = 0;
        int j = nums.length - 1;
        while (true){
            if (i>j){
                return -1;
            }
            int m = (int) (i+j)/2;
            int A = (int) nums[m];
            if (target < A){
                j = m - 1;
                continue;
            } else if (target > A) {
                i = m +1 ;
                continue;
            }else {
                return m;
            }
        }

    }
    // 测试用例！
    public static void main(String[] args) {
        int targetIndex = search(nums, 15);
        System.out.println("targetIndex = " + targetIndex);
    }
}
